package cc.rboot.inject.template;

import cc.rboot.inject.annotation.InjectBody;
import cc.rboot.inject.core.AbstractMapperInjector;
import cc.rboot.inject.helper.InjectMapperHelper;
import cc.rboot.inject.helper.InjectServiceHelper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

@RestController
@RequestMapping(path = "/_inject")
public class InjectPatch<T> extends AbstractMapperInjector<T> {

    /**
     * 默认的部分修改接口
     *
     * @param id
     * @param newEntity
     */
    @PatchMapping(path = "/{id}")
    public T _execute(@PathVariable(value = "id", required = true) Serializable id, @Validated @InjectBody T newEntity) throws Exception {
        InjectMapperHelper.fillId(newEntity, id);
        super.getService().updateById(newEntity);
        return newEntity;
    }

}
